//
//  InterfaceResultParser.m
//  Jared_AFN_Cache
//
//  Created by Jared on 2017/1/20.
//  Copyright © 2017年 Jared. All rights reserved.
//

#import "InterfaceResultParser.h"
#import "CommonResponseBody.h"
#import "CommonResponseBean.h"

#import "InterfaceResultModel.h"

#define nilOrJSONObjectForKey(JSON_, KEY_) [[JSON_ objectForKey:KEY_] isKindOfClass:[NSNull class]] ? nil : [JSON_ objectForKey:KEY_]
static NSString *CODE = @"200";
static NSString *ERRORCODE = @"400";


@implementation InterfaceResultParser

+ (id)getResponseBodyWithListDataFromJson:(NSString *)url header:(NSDictionary *)header resultJson:(id)resultJson{
    CommonResponseBody *responseBody = [[CommonResponseBody alloc] init];
    NSDictionary *jsonAllDict = (NSDictionary *)resultJson;
    CommonResponseBean *resultBean = [[CommonResponseBean alloc] init];
    resultBean.code = nilOrJSONObjectForKey(jsonAllDict, @"code");
    if ([resultBean.code isEqualToString:CODE]) {
        resultBean.message = nilOrJSONObjectForKey(jsonAllDict, @"message");
        id responseContent = nil;
        id responseData = nilOrJSONObjectForKey(jsonAllDict, @"data");
        NSArray *jsonArray = (NSArray *)responseData;
        //判断接口
        if ([url rangeOfString:@"" options:NSRegularExpressionSearch].location != NSNotFound) {
            //解析接口data数据到model
            responseContent  = [InterfaceResultModel paserResult:jsonArray];
        }else if ([url rangeOfString:@"http://appapi.makeronly.com/MKLService/featuredfirst/banner" options:NSRegularExpressionSearch].location != NSNotFound){
            responseContent = [InterfaceResultModel paserResult:jsonArray];
        }
        if (responseContent) {
            resultBean.data = responseContent;
            responseBody.responseBean = resultBean;
        }
    }else{
        CommonResponseBean *errorBean = [self generateErrorBean];
        responseBody.responseBean = errorBean;
    }
    return responseBody;
}

+ (id)getResponseBodyWithDataFromJson:(NSString *)url header:(NSDictionary *)header resultJson:(id)resultJson{
    CommonResponseBody *responseBody = [[CommonResponseBody alloc] init];
    NSDictionary *jsonAllDict = (NSDictionary *)resultJson;
    NSLog(@"jsonAllDict===%@", jsonAllDict);
    CommonResponseBean *resultBean = [[CommonResponseBean alloc] init];
    resultBean.code = nilOrJSONObjectForKey(jsonAllDict, @"code");
//    if ([resultBean.code isEqualToString:CODE]) {
        resultBean.message = nilOrJSONObjectForKey(jsonAllDict, @"message");
        id responseContent = nil;
        id responseData = nilOrJSONObjectForKey(jsonAllDict, @"data");
        NSDictionary *jsonDict = (NSDictionary *)responseData;
        //判断接口
        if ([url rangeOfString:@"" options:NSRegularExpressionSearch].location != NSNotFound) {
            responseContent = [InterfaceResultModel paserResultWithData:jsonDict];
        }else if ([url rangeOfString:@"http://appapi.makeronly.com/MKLService/featuredfirst/banner" options:NSRegularExpressionSearch].location != NSNotFound){
            responseContent = [InterfaceResultModel paserResultWithData:jsonDict];
        }else if ([url rangeOfString:@"http://api2.pianke.me/timeline/list" options:NSRegularExpressionSearch].location != NSNotFound){
            responseContent = [InterfaceResultModel paserResultToModelWithData:jsonDict];
        }
        if (responseContent) {
            resultBean.data = responseContent;
            responseBody.responseBean = resultBean;
        }
//    }else{
//        CommonResponseBean *errorBean = [self generateErrorBean];
//        responseBody.responseBean = errorBean;
//    }
    return responseBody;
}



+ (CommonResponseBean *)generateErrorBean{
    CommonResponseBean *errorBean = [[CommonResponseBean alloc] init];
    errorBean.code = ERRORCODE;
    errorBean.data = @"Jared,there are problems!!!!!";
    return errorBean;
}

@end





